package sealed_generator.card;

import java.util.Arrays;

public class ManaCost {
	public static final int colorless=0, W=1, U=2, B=3, R=4, G=5,
		hWU=6, hUW=6, hWB=7, hBW=7, hWR=8, hRW=8, hUB=9, hBU=9, hUR=10, hRU=10,
		hUG=11, hGU=11, hBR=12, hRB=12, hBG=13, hGB=13, hRG=14, hGR=14, hWG=15, hGW=15,
		phyW=16, phyU=17, phyB=18, phyR=19, phyG=20, snow=21;
	private int cmc;
	private int[] parts;

	public ManaCost(String mana){
		parts = new int[22];
		for (int i = 0; i < mana.length(); i++){
			char c = mana.toUpperCase().charAt(i);
			switch(c){
			case 'W':parts[W]++; break;
			case 'U':parts[U]++; break;
			case 'B':parts[B]++; break;
			case 'R':parts[R]++; break;
			case 'G':parts[G]++; break;
			case 'D':parts[hWU]++; break;
			case 'V':parts[hUB]++; break;
			case 'K':parts[hBR]++; break;
			case 'L':parts[hGR]++; break;
			case 'A':parts[hGW]++; break;
			case 'O':parts[hWB]++; break;
			case 'I':parts[hUR]++; break;
			case 'Q':parts[hGB]++; break;
			case 'P':parts[hWR]++; break;
			case 'S':parts[hUG]++; break;
			case '!':parts[phyW]++; break;
			case '@':parts[phyG]++; break;
			case '`':parts[phyU]++; break;
			case '$':parts[phyB]++; break;
			case '^':parts[phyR]++; break;
			case 'N':parts[snow]++; break;
			default:
				String colorlessMana = "";
				try{
					while(i < mana.length()){
						colorlessMana += Integer.parseInt(Character.toString(mana.toUpperCase().charAt(i)));
						i++;
					}
				} catch(NumberFormatException nfe){
					
				}
				
				if (!colorlessMana.equals("")){
					parts[colorless] += Integer.parseInt(colorlessMana);
					i--;			
				}
			}
		}
	}
	
	public int compareTo(ManaCost c){
		int[] otherCost = c.getManaAmounts();
		if (cmc < c.getCMC())
			return -1;
		if (cmc > c.getCMC())
			return 1;
		for (int i = 0; i < parts.length; i++){
			if (parts[i] < otherCost[i])
				return -1;
			if (parts[i] > otherCost[i])
				return 1;
		}
		return 0;
	}
	
	public boolean equals(ManaCost c){
		int[] otherParts = c.getManaAmounts();
		for (int i = 0; i < parts.length; i++){
			if (parts[i] != otherParts[i])
				return false;
		}
		
		return true;
	}
	
	public String toString(){
		String ret = "";
		if(parts[colorless] != 0){
			ret = Integer.toString(parts[colorless]);
		}
		for(int i = 0; i < parts[W]; i++)
			ret += "W";
		for(int i = 0; i < parts[U]; i++)
			ret += "U";
		for(int i = 0; i < parts[B]; i++)
			ret += "B";
		for(int i = 0; i < parts[R]; i++)
			ret += "R";
		for(int i = 0; i < parts[G]; i++)
			ret += "G";
		for(int i = 0; i < parts[hWU]; i++)
			ret += "%D";
		for(int i = 0; i < parts[hUB]; i++)
			ret += "%V";
		for(int i = 0; i < parts[hBR]; i++)
			ret += "%K";
		for(int i = 0; i < parts[hGR]; i++)
			ret += "%L";
		for(int i = 0; i < parts[hGW]; i++)
			ret += "%A";
		for(int i = 0; i < parts[hWB]; i++)
			ret += "%O";
		for(int i = 0; i < parts[hUR]; i++)
			ret += "%I";
		for(int i = 0; i < parts[hGB]; i++)
			ret += "%Q";
		for(int i = 0; i < parts[hWR]; i++)
			ret += "%P";
		for(int i = 0; i < parts[hUG]; i++)
			ret += "%S";
		for(int i = 0; i < parts[phyW]; i++)
			ret += "%!";
		for(int i = 0; i < parts[phyG]; i++)
			ret += "%@";
		for(int i = 0; i < parts[phyU]; i++)
			ret += "%`";
		for(int i = 0; i < parts[phyB]; i++)
			ret += "%$";
		for(int i = 0; i < parts[phyR]; i++)
			ret += "%^";
		for(int i = 0; i < parts[snow]; i++)
			ret += "%N";
		return ret;
	}
	
	public int[] getManaAmounts(){
		return Arrays.copyOf(parts, parts.length);
	}
	public int getCMC(){return cmc;}
}
